﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net;
using System.IO;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.Text.RegularExpressions;

namespace YahooSamples.flickr
{
    public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(txtcity.Text))
            {
                string yql = "SELECT * FROM flickr.photos.search WHERE has_geo='true' AND text='" + txtcity.Text.Trim() + "'";          
                string html = getYQL(yql);
                if (string.IsNullOrEmpty(html)) return;
                XDocument xml = XDocument.Parse(html);

                int i = 0;
                foreach (XElement el in xml.Descendants("photo"))
                {
                    //get photo page url
                    yql = "select * from flickr.photos.info where photo_id='" + el.Attribute("id").Value  + "'";
                    string html2 = getYQL(yql);
                    if (string.IsNullOrEmpty(html2)) return;
                    XDocument xml2 = XDocument.Parse(html2);
                    string photourl = xml2.Root.Element("results").Element("photo").Element("urls").Element("url").Value;

                    //get photo page content
                    html2 = getURL(photourl);
                    if (string.IsNullOrEmpty(html2)) return;

                    Match c = Regex.Match(html2, "link rel=\"image_src\" href=\"(.+)\"");
                    if (c.Success)
                    {
                        GroupCollection g = c.Groups;
                        string realurl = g[1].Captures[0].ToString();
                        if (i == 0)
                        {
                            img1.ImageUrl = realurl;
                            img1.ToolTip = xml2.Root.Element("results").Element("photo").Element("title").Value;
                        }
                        else if (i == 1)
                        {
                            img2.ImageUrl = realurl;
                            img2.ToolTip = xml2.Root.Element("results").Element("photo").Element("title").Value;
                        }
                        else if (i == 2)
                        {
                            img3.ImageUrl = realurl;
                            img3.ToolTip = xml2.Root.Element("results").Element("photo").Element("title").Value;                        
                        }
                        else if (i == 3)
                        {
                            img4.ImageUrl = realurl;
                            img4.ToolTip = xml2.Root.Element("results").Element("photo").Element("title").Value;
                        }
                        else if (i == 4)
                        {
                            img5.ImageUrl = realurl;
                            img5.ToolTip = xml2.Root.Element("results").Element("photo").Element("title").Value;
                        }
                        else if (i == 5)
                        {
                            img6.ImageUrl = realurl;
                            img6.ToolTip = xml2.Root.Element("results").Element("photo").Element("title").Value; 
                        }
                    }
                    i++;

                    if (i > 5) return;
                }

                
            }
        }

        private string getYQL(string yql)
        {
            string url = "http://query.yahooapis.com/v1/public/yql?q=" + HttpUtility.UrlEncode(yql).Replace("+", "%20").Replace("*", "%2A").Replace("(", "%28").Replace(")", "%29"); ;
            return getURL(url);
        }

        private string getURL(string url)
        {
            string body = "";
            try
            {
                HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
                WebResponse resp = req.GetResponse();
                Encoding enc = System.Text.Encoding.GetEncoding(1252);
                StreamReader loResponseStream = new
                StreamReader(resp.GetResponseStream(), enc);
                body = loResponseStream.ReadToEnd();
            }
            catch (WebException we)
            {
            }

            return body;
        }


    }
}